package com.mlz.mlzmall.ui.adapter.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

/**
 * 功能描述：MallHolder
 * Created by benny on 2016/5/7.
 * ProjectName：mlzMall.
 */
public class MallHolder {
    private SparseArray<View> views;
    private Context context;
    private int position;
    private View convertView;

    public MallHolder() {
    }

    public MallHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.context = context;
        this.views = new SparseArray<View>();
        this.convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        this.convertView.setTag(this);
    }

    public static MallHolder getViewHolder(Context context, View convertView, ViewGroup parent,
                                           int layoutId, int position) {
        if (convertView == null) {
            return new MallHolder(context, parent, layoutId, position);
        } else {
            MallHolder holder = (MallHolder) convertView.getTag();
            holder.position = position;
            return holder;
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T getItemView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = this.convertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * @param @param  viewId TextView ID
     * @param @param  content内容
     * @param @return
     * @throws
     * @Title: setText
     * @Description: 设置TextView内容
     * @return:ViewHolder
     * @user:laona
     * @date 2015-4-19 下午1:19:22
     */
    public MallHolder setText(int viewId, String content) {
        TextView tv = getItemView(viewId);
        tv.setText(isNull(content));
        return this;
    }

    private String isNull(String content) {
        if (content == null) {
            return "";
        } else {
            return content;
        }
    }

    /**
     * @param @param  viewId
     * @param @param  content
     * @param @return
     * @throws
     * @Title: setNetworkImageView
     * @Description: 设置NetworkImageView图片
     * @return:ViewHolder
     * @user:laona
     * @date 2015-4-19 下午1:19:48
     */
    public MallHolder setNetworkImageView(int viewId, int defaultImgId, int errorImgId, String url,
                                          ImageLoader imageLoader) {
        NetworkImageView img = getItemView(viewId);
        img.setDefaultImageResId(defaultImgId);
        img.setErrorImageResId(errorImgId);
        img.setImageUrl(url, imageLoader);
        return this;
    }

    public int getPosition() {
        return this.position;
    }

    public View getConvertView() {
        return this.convertView;
    }


}
